#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: zhengpingping

import sys
import xlrd3
import importlib
import ExcelRead
importlib.reload(sys)

outPath = '../../StarForce/Assets/Scripts/CSharp/Data/Table/'
#内部使用
search_names = []

#获取字段类型名称
def get_type_name(typeRow):
    if typeRow == 'bool':
        return 'bool'
    elif typeRow == 'int':
        return 'int'
    elif typeRow == 'array_int':
        return 'List<int>'
    else:
        return 'string'

#生成c#代码
def create_item_CSCode(ws):
    search_line = ExcelRead.excel_sheet_read(ws)
    if(len(search_line) <= 0):
        print(ws.name + '  data is empty')
        return

    begin_line = search_line[0]
    begin_type = get_type_name(begin_line.fieldType)

    if(begin_type != 'int' and begin_type != 'string'):
        print(begin_line.fieldName + ' type can only is int or string')
        return

    string = ('using System.Collections.Generic;')
    string += ('\r\n')
    string += ('\r\n') + ('public class Table_@NameItem : IDataItem')
    string += ('\r\n') + ('{')

    #生成数据
    for line in search_line:
        string += ('\r\n\t') + ('/// <summary>')
        string += ('\r\n\t') + ('/// ') + line.fieldDesc
        string += ('\r\n\t') + ('/// <summary>')

        if line.fieldType == 'bool':
            string += ('\r\n\t') + ('public bool @NameRow { get; private set; }')
        elif line.fieldType == 'int':
            string += ('\r\n\t') + ('public int @NameRow { get; private set; }')
        elif line.fieldType == 'array_int':
            string += ('\r\n\t') + ('public int Get@NameRowCount() { return @NameRow.Count; }')
            string += ('\r\n\t') + ('private List<int> @NameRow = new List<int>();')
            string += ('\r\n\t') + ('public int Get@NameRowByIndex(int idx)')

            string += ('\r\n\t') + ('{')
            string += ('\r\n\t\t') + ('return @NameRow.GetValueByIdx(idx, 0);')
            string += ('\r\n\t') + ('}')
        else:
            string += ('\r\n\t') + ('public string @NameRow { get; private set; }')
        string = string.replace("@NameRow", line.fieldName)

    string += ('\r\n')
    string += ('\r\n\t') + ('public Table_@NameItem(ExcelLineReader reader)')
    string += ('\r\n\t') + ('{')

    #读取数据
    for line in search_line:
        if line.fieldType == 'bool':
            string += ('\r\n\t\t') + ('@NameRow = reader.ReadBool();')
        elif line.fieldType == 'int':
            string += ('\r\n\t\t') + ('@NameRow = reader.ReadInt();')
        elif line.fieldType == 'array_int':
            string += ('\r\n\t\t') + ('@NameRow = reader.ReadShortString().ToIntList();')
        else:
            string += ('\r\n\t\t') + ('@NameRow = reader.ReadString();')
        string = string.replace("@NameRow", line.fieldName)

    string += ('\r\n\t') + ('}')
    string += ('\r\n') + ('}')

    string += ('\r\n')
    string += ('\r\n') + ('public class Table_@NameMgr : IDataMgr')
    string += ('\r\n') + ('{')
    string += ('\r\n\t') + ('protected override string FileName { get { return "Txt/@Name.txt"; } }')

    string += ('\r\n')
    string += ('\r\n\t') + ('protected Dictionary<@begin_type, int> mItemDic = new Dictionary<@begin_type, int>();')
    string += ('\r\n\t') + ('public @begin_type GetIdxById(@begin_type key) { return mItemDic[key]; }')

    string += ('\r\n')
    string += ('\r\n\t') + ('public override void Load(ExcelLineReader reader)')
    string += ('\r\n\t') + ('{')
    string += ('\r\n\t\t') + ('var item = new Table_@NameItem(reader);')
    string += ('\r\n\t\t') + ('mItemDic[item.@begin_name] = DataCount;')
    string += ('\r\n\t\t') + ('mItemList.Add(item);')
    string += ('\r\n\t') + ('}')
    
    string += ('\r\n')
    string += ('\r\n\t') + ('public Table_@NameItem GetItemByIdx(int idx)')
    string += ('\r\n\t') + ('{')
    string += ('\r\n\t\t') + ('return GetItemByIdx<Table_@NameItem>(idx);')
    string += ('\r\n\t') + ('}')

    string += ('\r\n')
    string += ('\r\n\t') + ('public Table_@NameItem GetItemById(int id)')
    string += ('\r\n\t') + ('{')
    string += ('\r\n\t\t') + ('int idx = GetIdxById(id);')
    string += ('\r\n\t\t') + ('return GetItemByIdx(idx);')
    string += ('\r\n\t') + ('}')
    string += ('\r\n') + ('}')

    string = string.replace("@begin_type", begin_type)
    string = string.replace("@begin_name", begin_line.fieldName)
    string = string.replace("@Name", ws.name)

    search_names.append(ws.name)
    outputName = ('Table_@NameMgr.cs').replace("@Name", ws.name)
    ExcelRead.save_file(outPath + outputName, string)

#生成mgr代码
def create_mgr_CSCode():
    string = ('public class TableDataMgr')
    string += ('\r\n') + ('{')

    for name in search_names:
        temp = 'public static Table_@NameMgr @NameMgr = new Table_@NameMgr();'
        string += ('\r\n\t') + temp.replace("@Name", name)

    string += ('\r\n')
    string += ('\r\n\t') + ('public TableDataMgr()')
    string += ('\r\n\t') + ('{')

    for name in search_names:
        string += ('\r\n\t\t') + ('@NameMgr.Init();').replace("@Name", name)

    string += ('\r\n\t') + ('}')
    string += ('\r\n') + ('}')

    outputName = outPath + 'TableDataMgr.cs'
    ExcelRead.save_file(outputName, string)

# 读取excel文件
def excel_sheet_read(file):
    wb = xlrd3.open_workbook(file)
    for sheet in wb.sheets():
        print(sheet.name + ' translate start')
        if(ExcelRead.check_fileName(sheet.name, search_names)):
            create_item_CSCode(sheet)

# 初始化方法
ExcelRead.init(outPath, excel_sheet_read)
create_mgr_CSCode()
print('TableDataMgr translate finished')